package com.fzrskj.cloud.free.user.service.manage;

import com.alibaba.fastjson.JSONObject;
import com.fzrskj.cloud.free.core.WiniisCacheImplMain;
import com.fzrskj.cloud.free.core.enums.DicParamEnum;
import com.fzrskj.cloud.free.core.enums.UserStatusEnum;
import com.fzrskj.cloud.free.core.tool.CEncryptTool;
import com.fzrskj.cloud.free.core.tool.CSessionTool;
import com.fzrskj.cloud.free.core.utils.EncryptUtils;
import com.fzrskj.cloud.free.dic.utils.DicParamUtils;
import com.fzrskj.cloud.free.sys.bean.Cookies;
import com.fzrskj.cloud.free.user.bean.UserAccount;
import com.fzrskj.cloud.free.user.utils.UserAccountUtils;
import com.winiis.component.cache.core.CacheUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.support.Session;
import ms.core.tool.CookieTool;
import ms.core.tool.JsonTool;
import ms.core.tool.XmlCodeParser;
import org.apache.commons.collections.map.HashedMap;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * UserAccountService.
 *
 * @author zj.
 *         Created on 2019/10/11 0011.
 */
public class UserAccountService extends CObject {
    private String manageSessionKey = "chg_m:{sid}";
    private Integer timeout = 86400;

    /**
     * 用户身份切换.
     */
    public String changeUser (HttpServletRequest req, HttpServletResponse resp, String p, String sid)  {
        String username = JsonTool.getStrValue(p, "username", "");

        //查询用户是否存在
        UserAccountUtils accountUtils = new UserAccountUtils();
        UserAccount userAccount = accountUtils.queryAccountByName(username);
        if (null == userAccount) {
            return XmlCodeParser.formatResultTips(false, "USER NOT FOUND", "用户不存存或者是下级代理的用户");
        }

        if (!UserStatusEnum.NORMAL.equals(userAccount.getStatus())) {
            return XmlCodeParser.formatResultTips(false, "USER STATUS ERROR", "该用户为不可用状态");
        }

        //缓存记忆管理员账号信息
        CacheUtils cacheUtils = new CacheUtils(new WiniisCacheImplMain());
        cacheUtils.set(this.manageSessionKey.replaceAll("\\{sid}", sid), CSessionTool.getSessionCloud(sid), this.timeout);

        //切换session为用户的信息
        Session ssn = new Session();
        ssn.setId(userAccount.getUserId());
        ssn.setUID(userAccount.getUserId());
        ssn.setUname(userAccount.getUserName());
        ssn.setData(JSONObject.toJSONString(userAccount));
        ssn.setUtype(1);
        //TODO to fill user functions
        ssn.setFuncs("");
        CSessionTool.setLoginSessionCloud(sid, ssn);

        //设置Cookie
        Cookies cks = new Cookies();
        cks.setId(userAccount.getId());
        cks.setUid(userAccount.getUserId());
        cks.setAccount(userAccount.getUserName());
        cks.setUname(userAccount.getUserName());
        cks.setSaid(CEncryptTool.encodeSAID(sid, userAccount.getUserId(), userAccount.getUserName()));
        CookieTool.setLoginCookie(resp, sid, cks);

        Cookie cookie = new Cookie("bg_admin", "y");
        resp.addCookie(cookie);

        EncryptUtils.makeSession(resp, req);

        //返回前端
        String template = DicParamUtils.getParamValue(DicParamEnum.TEMPLATE_KEY.getVal());
        Map<String, Object> data = new HashedMap();
        data.put("template", template);
        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JSONObject.toJSONString(data));
    }

    public String backAdmin (HttpServletRequest req, HttpServletResponse resp, String p, String sid)  {
        //缓存记忆管理员账号信息
        CacheUtils cacheUtils = new CacheUtils(new WiniisCacheImplMain());
        Session session = cacheUtils.get(this.manageSessionKey.replaceAll("\\{sid}", sid));
        if (null == session) {
            return XmlCodeParser.formatResultTips(false, "ADMIN NOT FOUND", "管理员用户不存在");
        }

        //切换session为管理用户的信息
        CSessionTool.setLoginSessionCloud(sid, session);

        //设置Cookie
        Cookies cks = new Cookies();
        cks.setId(session.getId());
        cks.setUid(session.getUID());
        cks.setAccount(session.getUname());
        cks.setUname(session.getUname());
        cks.setSaid(CEncryptTool.encodeSAID(sid, session.getUID(), session.getUname()));
        CookieTool.setLoginCookie(resp, sid, cks);

        EncryptUtils.makeSession(resp, req);

        return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
    }
}
